/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package core.modules.communication.test;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import javax.management.MBeanServer;

import org.jboss.remoting.Client;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvocationHandler;
import org.jboss.remoting.ServerInvoker;
import org.jboss.remoting.callback.Callback;
import org.jboss.remoting.callback.HandleCallbackException;
import org.jboss.remoting.callback.InvokerCallbackHandler;
import org.jboss.remoting.transport.Connector;

import com.sun.org.apache.bcel.internal.generic.InstructionConstants.Clinit;

/**
 * This class and org.jboss.remoting.samples.bisocket.BisocketSampleClient
 * demonstrate how to how to make an invocation and how to set up push callbacks
 * over the bisocket transport.
 * 
 * The reason for the existance of the bisocket transport, which is derived from
 * the socket transport, is to make it possible to do push callbacks to a client
 * which is unable to create a ServerSocket, either due to security restrictions
 * or due to the fact that it is behind a firewall.
 * 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 * @version $Revision: 1.1 $
 *          <p>
 *          Copyright May 1, 2008
 *          </p>
 */
public class BisocketSampleServer {
	public static int port = 4567;

	protected Connector connector;
	private InvokerLocator locator;
	protected static BisocketSampleServer server;
	static HashMap<String, Client> clients = new HashMap<String, Client>();
	String serverLocatorURI = "socket://192.168.1.12:8888";

	protected void setupServer() throws Exception {
		// Create the InvokerLocator based on url string format
		// to indicate the transport, host, and port to use for the
		// server invoker.
		locator = new InvokerLocator(serverLocatorURI);

		connector = new Connector(locator);
		// Creates all the connector's needed resources, such as the server
		// invoker.
		connector.create();
		// Create the handler to receive the invocation request from the client
		// for processing.
		SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
		connector.addInvocationHandler("MYSYS", invocationHandler);
		// Start server.
		connector.start();
		System.out.println("Started remoting server with locator uri of: "
				+ serverLocatorURI);
	}

	protected void setupClient(String clientLocatorURI) throws Exception {
		Client client = new Client(new InvokerLocator(clientLocatorURI), "MYSYS");
		clients.put(clientLocatorURI, client);
		System.out.println("Connecting");
		client.connect();
		while (!client.isConnected())
			client.connect();
		if (client.isConnected())
			System.out.println("Connected");
	}

	protected void shutdownServer() throws Exception {
		if (connector != null) {
			connector.stop();
			System.out.println("shut down server");
		}
	}

	public static void main(String[] args) {
		try {
			// Create and start server.
//			String host = InetAddress.getLocalHost().getHostAddress();
			server = new BisocketSampleServer();
			server.setupServer();
			server.setupClient("socket://192.168.1.11:8889");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Invocations are handled by the ServerInvocationHandler.
	 */
	static class SampleInvocationHandler implements ServerInvocationHandler {
		ArrayList<String> stringList = new ArrayList<String>();

		public void addListener(InvokerCallbackHandler callbackHandler) {
		}

		public Object invoke(final InvocationRequest invocation)
				throws Throwable {
			Message message = (Message) invocation.getParameter();
			Client client = clients.get(message.locatorURI);
			if (stringList.size() < 10) {
//				Object param = invocation.getParameter();
//				String s = (String) param + "s";
				String s = message.value + "s";
				stringList.add(s);
				return s;
			} else {
				System.out.println("INVOKE");
				client.invoke(stringList);
				return null;
			}
		}

		public void removeListener(InvokerCallbackHandler callbackHandler) {
		}

		public void setMBeanServer(MBeanServer server) {
		}

		public void setInvoker(ServerInvoker invoker) {
		}
	}
}